package com.xl.mananger;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.xl.bean.ActionMapping;
import com.xl.bean.ModelMapping;
import com.xl.bean.ResultView;
/**
 * 
 * @author 深圳-夕落  2014-11-25
 * 封装XML的类
 */
public class MappingManager {
	/**
	 * 保存Action信息
	 */
	static Map<String,Map<String,ActionMapping>> actionMappingMap = new HashMap<String,Map<String,ActionMapping>>();
	/**
	 * 保存实体类
	 */
	static Map<String,ModelMapping> ModelMappingMap = new HashMap<String,ModelMapping>();
	/**
	 * 
	 * @param filePaths 配置文件的路径数组 
	 * 解析配置文件 全部存入map中
	 */
	public static void inIt(String [] filePaths)
	{
		InputStream in = null;
		Document document = null;
		for(String filePath : filePaths)
		{
			//获取文件的输入流
			in = MappingManager.class.getClassLoader().getResourceAsStream(filePath);
			try 
			{
				//获取配置文件的文档对象
				document = new SAXReader().read(in);
				
			} catch (DocumentException e) 
			{				
				e.printStackTrace();
			}
			if(filePath.contains("action"))
			{   //action的配置文件交给该方法处理
				getActionMappings( document);
				
			}else if(filePath.contains("bean"))
			{
				//实体类的配置文件交给该方法处理
				getModelMappings( document);
			}
		}
		try 
		{
			  in.close();
			  
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 保存实体类的信息
	 * @param document
	 */
	public static void getModelMappings(Document document)
	{
		//获取文件的根节点
		Element root = document.getRootElement();
		//获取beans节点
		Element beans = (Element)root.elementIterator().next();
		//获取bean节点
		List<Element> beanList =  beans.elements();
		//声明一个model配置实体类
		ModelMapping modelMapping = null;
		//循环将值保存在map中
		for(Element e : beanList)
		{
			modelMapping = new ModelMapping(e.attributeValue("name"),e.attributeValue("class"));
			ModelMappingMap.put(e.attributeValue("name"), modelMapping);
		}
	}
	
	/**
	 * 保存Action的信息
	 * @param document
	 */
	public static void getActionMappings(Document document)
	{		
	    //获取文件的根节点
		Element root = document.getRootElement();
		//获取package节点
		List<Element> packageElement = root.elements();
		
		Map<String,ActionMapping> actionmappings = null;
		//遍历循环获取Action节点
		for(int i = 0; i < packageElement.size(); i ++)
		{
			
		    actionmappings = new HashMap<String,ActionMapping>();
		    List<Element> actions =  packageElement.get(i).elements("action");
		    ActionMapping actionMapping  = null;
		  //遍历循环获取result节点
		    for(int j = 0; j < actions.size(); j++)
		    {
				List<Element> results = actions.get(j).elements("result");
				Map<String,ResultView> resultMap = new HashMap<String,ResultView>();
				ResultView result = null;
				for(int z = 0; z < results.size(); z ++)
				{
					result = new ResultView(results.get(z).attributeValue("name"),
							                 results.get(z).getText(),
							                 null != results.get(z).attributeValue("type") && results.get(z).attributeValue("type").equals("redirect"));
					resultMap.put(result.getName(), result);
				}
				
				actionMapping = new ActionMapping(actions.get(j).attributeValue("name"),
                        actions.get(j).attributeValue("class"),
                        resultMap);
				actionmappings.put(actionMapping.getActionName(), actionMapping);
		}
		actionMappingMap.put(packageElement.get(i).attributeValue("name"), actionmappings);
	}
	
 }
	
	/**
	 * 获取action的封装类
	 * @param packageName
	 * @param actionName
	 * @return
	 */
     public  static ActionMapping getActionValue(String packageName,String actionName )
     {
    	 if(null != packageName && null != actionName && null != actionMappingMap.get(packageName)
    			 && null != actionMappingMap.get(packageName).get(actionName))
    	 {
    		 
    		 return actionMappingMap.get(packageName).get(actionName);
    	 }else 
    	 {
    		 
				try
				{
					throw new Exception(actionName + "action 不存在");
					
				} catch (Exception e) 
				{
					e.printStackTrace();
				}
				
				return null;
    	 }
		
		
	 }
     /**
      * 获取Model的封装类
      * @param beanName
      * @return
      */
     public static ModelMapping getModelValue(String ModelName){
    	
    	 if(null != ModelName && null != ModelMappingMap.get(ModelName))
    	 {
    		 return ModelMappingMap.get(ModelName);
    	 }else 
    	 {
    		 try 
    		 {  			 
				throw new Exception(ModelName+"实体类不存在");
				
			} catch (Exception e) 
			{				
				e.printStackTrace();
			}
    		 return null;
    	 }
    	
     }
	
     /**
      * 截取需要的路径
      * @param request
      * @return
      */
     public static  String[] getPaht(HttpServletRequest request)
 	{
 		return request.getServletPath().substring(1, request.getServletPath().indexOf(".")).split("/");
 	}
}
